package com.dq.domain.goods;

import com.dq.vo.baseVo.VO;
import com.dq.domain.base.BaseEntity;
import com.dq.domain.orders.info.OrderInfo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import java.math.BigDecimal;

/**
 * 购物商品袋
 */
@Data
@Entity
@ApiModel("购物袋")
public class ShoppingBag extends BaseEntity implements VO.ToVO {

    @ManyToOne
    @ApiModelProperty("产品")
    protected Product product;
    @ManyToOne
    @ApiModelProperty("商品")
    protected Goods goods;
    @ManyToOne
    @ApiModelProperty("关联购物车")
    protected ShoppingCart cart;
    @ApiModelProperty("数量")
    protected Long number;

    public void plus(Long n) {
        this.number = n + this.number;
    }

    @Override
    public Vo toVO(boolean isRecursion) {
        Vo vo = new Vo();
        vo.id = this.id;
        vo.productVo = null == this.product ? null : (Product.Vo) this.product.toVO(false);
        vo.number = fmt(this.number);
        vo.goodVo = null == this.goods ? null : (Goods.Vo) this.goods.toVO(false);
        vo.cartId = this.cart.getId();
        vo.price = this.product != null ? fmt(this.product.getPrice()) : BigDecimal.ZERO;
        vo.listImgUrl = this.goods != null ? fmt(this.goods.getListImgUrl()) : "";
        vo.sku = this.product != null ? fmt(this.product.getSku()) : "";
        vo.goodName = this.goods != null ? fmt(this.goods.getGoodsName()) : "";
        return vo;
    }

    @Data
    @ApiModel("购物袋视图")
    public static class Vo implements VO {
        @ApiModelProperty("购物袋id")
        private Long id;
        @ApiModelProperty("产品")
        private Product.Vo productVo;
        @ApiModelProperty("产品数量")
        private Long number;
        @ApiModelProperty("商品")
        private Goods.Vo goodVo;
        @ApiModelProperty("购物车id")
        private Long cartId;
        @ApiModelProperty("商品价格")
        private BigDecimal price;
        @ApiModelProperty("商品图片")
        private String listImgUrl;
        @ApiModelProperty("sku")
        private String sku;
        @ApiModelProperty("商品名称")
        private String goodName;
    }
    public OrderInfo toOrderInfo() {
        OrderInfo info = new OrderInfo();
        info.setGoods(this.goods);
        info.setProduct(this.product);
        info.setNumber(fmt(this.number));
        if (this.product != null) {
            info.setAmount(fmt(BigDecimal.valueOf(this.number).multiply(this.product.getPrice())));
        } else {
            info.setAmount(fmt(BigDecimal.valueOf(this.number).multiply(this.goods.getPrice())));
        }
        return info;
    }
}
